#!/usr/bin/env bash

#=======================================================================#
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com>       #
#                                                                       #
# This file is part of KIAUH - Klipper Installation And Update Helper   #
# https://github.com/dw-0/kiauh                                         #
#                                                                       #
# This file may be distributed under the terms of the GNU GPLv3 license #
#=======================================================================#

set -e

function install_ui() {
  top_border
  echo -e "|     ${green}~~~~~~~~~~~ [ 安装菜单 ] ~~~~~~~~~~~${white}     |"
  hr
  echo -e "|  You need this menu usually only for installing       |"
  echo -e "|  all necessary dependencies for the various           |"
  echo -e "|  functions on a completely fresh system.              |"
  echo -e "|  请输入对应数字来安装对应的程序              |"  
  hr
  echo -e "| Firmware & API:                | Other:                     |"
  echo -e "|  1) [Klipper] (打印主程序)      |  7) [PrettyGCode]          |"
  echo -e "|  2) [Moonraker] (后台程序)      |  8) [Telegram Bot]         |"
  echo -e "|                                 |  9) $(obico_install_title) |"
  echo -e "| Klipper Webinterface:           | 10) [OctoEverywhere]       |"
  echo -e "|  3) [Mainsail] (网页界面)        | 11) [Mobileraker]          |"
  echo -e "|  4) [Fluidd] (网页界面推荐)      | 12) [OctoApp for Klipper]  |"
  echo -e "|                                 | 13) [Spoolman] (耗材管理)  |"
  echo -e "| Touchscreen GUI:                | 14) [Happy-Hare] (多色快乐兔)|"
  echo -e "|  5) [KlipperScreen](打印显示屏幕) |15) [klippain-shaketune](共振补偿分析) |"
  echo -e "|                                   | Webcam Streamer:           |"
  echo -e "| 3rd Party Webinterface:           | 16) [Crowsnest]  (摄像头配置)  |"
  echo -e "|  6) [OctoPrint] (打印管理可不装)     |17) [moonraker-timelapse]  (延迟摄影)|"
  hr
  echo -e "|  18)[Easy Installation level1 (便捷一键安装) ](自动安装klipper,moonraker,fluidd基础打印套餐)|"
  echo -e "|  19)[Easy Installation level2 (便捷一键安装) ](自动安装klipper,moonraker,fluidd,klipperscreen带屏幕进阶打印套餐)|" 
  echo -e "|  20)[Easy Installation level3 (便捷一键安装) ](自动安装klipper,moonraker,fluidd,crowsnest摄像头进阶打印套餐)|" 
  back_footer
}

function install_menu() {
  clear -x && sudo true && clear -x # (re)cache sudo credentials so password prompt doesn't bork ui
  print_header
  install_ui

  ### save all installed webinterface ports to the ini file
  fetch_webui_ports

  ### save all klipper multi-instance names to the ini file
  set_multi_instance_names

  local action
  while true; do
    read -p "${cyan}####### Perform action:${white} " action
    case "${action}" in
      1)
        do_action "start_klipper_setup" "install_ui";;
      2)
        do_action "moonraker_setup_dialog" "install_ui";;
      3)
        do_action "install_mainsail" "install_ui";;
      4)
        do_action "install_fluidd" "install_ui";;
      5)
        do_action "install_klipperscreen" "install_ui";;
      6)
        do_action "octoprint_setup_dialog" "install_ui";;
      7)
        do_action "install_pgc_for_klipper" "install_ui";;
      8)
        do_action "telegram_bot_setup_dialog" "install_ui";;
      9)
        do_action "moonraker_obico_setup_dialog" "install_ui";;
      10)
        do_action "octoeverywhere_setup_dialog" "install_ui";;
      11)
        do_action "install_mobileraker" "install_ui";;
      12)
        do_action "octoapp_setup_dialog" "install_ui";;
      13)
        do_action "install_spoolman" "install_ui";;
      14)
        do_action "install_happyhare" "install_ui";;
      15)
        do_action "install_shaketune" "install_ui";;
      16)
        do_action "install_crowsnest" "install_ui";;
      17)
        do_action "install_timelapse" "install_ui";;
      18)
        do_action "start_klipper_setup" "install_ui"
        do_action "moonraker_setup_dialog" "install_ui"
        do_action "install_fluidd" "install_ui";;
      19)
        do_action "start_klipper_setup" "install_ui"
        do_action "moonraker_setup_dialog" "install_ui"
        do_action "install_fluidd" "install_ui"
        do_action "install_klipperscreen" "install_ui";;
      20)
        do_action "start_klipper_setup" "install_ui"
        do_action "moonraker_setup_dialog" "install_ui"
        do_action "install_fluidd" "install_ui"
        do_action "install_crowsnest" "install_ui";;
      B|b)
        clear; main_menu; break;;
      *)
        deny_action "install_ui";;
    esac
  done
  install_menu
}
